<?php
include_once('commonfuncs.php');
include_once('function_absolute.php');
function genleft($links,$selected){
	foreach ($links as $link => $val){
		if($selected == $link){
			$class = "current_page_item";
			}else{
				$class="page_item";
				}
		
		echo "<li class=\"$class\"><a title=\"$val\" href=\"$link\">$val</a></li>";
		
		}
	}
	
	function loginleft($selected){
		genleft(array("members.php?p=login"=>"Login", "index.php?p=register"=>"Register", "index.php?p=forgotpassword" =>"Forgot Password"), $selected);
		
		}
			function loginleftadmin($selected){
		genleft(array("index.php?p=dashboard"=>"Update Ads", "index.php?p=users"=>"Users", "index.php?p=streams" =>"Stream", "index.php?p=content" =>"Content","index.php?p=membermail" =>"Member Mail","index.php?p=clearcache" =>"Clear Cache","../members.php?p=logout" =>"Logout"), $selected);
		
		}
		
	function loginleftmember($selected){
		genleft(array("members.php?p=dashboard"=>"Dashboard", "members.php?p=adsettings"=>"Ad Setting", "members.php?p=website" =>"Protect Stream", "members.php?p=changepassword" =>"Change Password", "members.php?p=logout" =>"Logout"), $selected);
		
		}
	
				  
				  
  function check_password($admin){
			  
				

	  if(!empty($_POST['login'])){  
		global $db;
		dbconnect();
		  $email =  $db->escape($_POST['email']);	
		  $password = $db->escape($_POST['password']);
		  
		  $check = check_empty($_POST['email'], "Enter Email",!empty($check) ? $check : 0);
		  $check = check_empty($password , "Enter Password", $check);
		  
		  if($check != "1"){
			  
		  echo $_POST['email'];
		  
		  if(adminuser == $_POST['email'] and adminpass == $_POST['password']){
		  $ss = new SecureSession();
	  $ss->check_browser = true;
	  $ss->check_ip_blocks = 2;
	  
	   $_SESSION['logged_in_admin'] = true;
	  $redirect = "siteadmin/channel.php?p=list";
	  
	  	$ss->secure_word = 'wiistream_';
	  $ss->regenerate_id = false;
	  $ss->Open();
	  $_SESSION['logged_in'] = true;

  
  //script
  redirect($redirect);
  //header('Location: $redirect');
			 
		   }else{
			   $text = "Password is wrong";
	  error($text);
			   
			   }
		  
		  }
		   
		  
	  
	  }
	  
			  }
			  
	
	
	function createwebsite(){
			

	if($_POST['websitead']){global $db;
dbconnect();
	$check = check_empty($_POST['websitename'] , "Enter website name", $check);	
	if($_POST['websitename']){
	$filterdomain = array("www","http://","https://", "/",",");
	foreach($filterdomain as $f){
		
	if(stristr($_POST['websitename'],$f)){
		error("Enter the proper URL format like 'example.com'. Enter only on website at a time. Dont add www"); $check =1 ;
		break;
		}
		
		}
	
	//if (!preg_match("/^[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/i",$_POST['websitename'])) {
		//error("Enter the proper URL format like 'example.com'. Enter only on website at a time"); $check =1 ;
		//}	
		}
	if($check != "1"){
			
			$data6['website_wl'] = $_POST['websitename'];
			$data6['user_wl'] = $_SESSION['user'];
			$db->query_insert("websitelist_wl", $data6);
			good("Added successfully!");
			}
	
	}
	
		}
	function clearcache(){
	$cachedir = '../content/cache/';
// Directory to cache files in (keep outside web root) 
if ($handle = @opendir($cachedir)) { while (false !== ($file = @readdir($handle))) { if ($file != '.' and $file != '..') { echo $file . ' deleted.<br>'; @unlink($cachedir . '/' . $file); } } @closedir($handle); }
	
		}
		
		
	function updateuser($type, $id){
	global $db;
dbconnect();

			$data6['is_active_usr'] = $type;
	$db->query_update("users_usr", $data6, "id_usr = '$id'");

	$data7['is_active_stm'] = $type;
	$db->query_update("stream_stm", $data7, "uid_stm = '$id'");
	}
	
		function updatestream($type, $id){
	global $db;
dbconnect();

				$data7['is_active_stm'] = $type;
	$db->query_update("stream_stm", $data7, "id_stm = '$id'");
	}
	
	function streamlist(){
		global $db;
dbconnect();
	
		$sql = "select * from  stream_stm order by date_stm desc limit 30";
$rows = $db->query($sql);
while ($record= $db->fetch_array($rows)) {
	if($record['is_active_stm'] == 1 || $record['is_active_stm'] == 3){$suspend = "suspend";}else{$suspend = "activate";}
	if($record['is_active_stm'] == 1 || $record['is_active_stm'] == 3){$suspend_s = "s_suspend";}else{$suspend_s= "s_activate";}
	$date = @date("Y-M-d", strtotime($record['date_stm']));
		if($record['is_active_stm'] == 1 ){$status = "<div class=\"good_msg\">Live</div>";}else if ($record['is_active_stm'] == 3){ $status = "<div class=\"bad_msg\">offline</div>";}else{ $status = "<div class=\"bad_msg\">Banned</div>";}
	echo "
	
<div class=\"wrap\">
<div class=\"login-left\"><a href='".domain."/?p=channeldetails&id=$record[u_stm]'>$record[u_stm]</a></div><div class=\"login-left\">$date</div><div class=\"login-viewers\">$record[viewers]</div><div class=\"login-viewers\"><a href='?p=$suspend_s&id=$record[id_stm]'>$suspend</a></div><div class=\"login-viewers\">$status</div>

</div>";
	}
		
		}
	
function userslist(){
global $db;
dbconnect();
	
		$sql = "select * from users_usr";
$rows = $db->query($sql);
while ($record= $db->fetch_array($rows)) {
	if($record[is_active_usr] == 1){$suspend = "suspend";}else{$suspend = "activate";}
	echo "
	<div class=\"wrap\">
	<div class=\"login-left\">$record[username_usr]</div><div class=\"login-left-left\">$record[email_usr]</div><div class=\"login-left\"><a href='?p=$suspend&id=$record[id_usr]'>$suspend</a></div></div>";
	}
		
		}
		
		
function udpatead($type=NULL){
	global $db;
dbconnect();

	
	if($_POST['changead']){
			$check = check_empty($_POST['codead'] , "Dont leave the ad area empty", $check);	
			
			if($check != "1"){

if($type == 1){
$data6['ad_ads'] =  get_magic_quotes_gpc()==1 ? stripslashes($_POST['codead']) :$_POST['codead'];
	
	$db->query_update("admin_ads", $data6, "type_ads = '$_POST[typead]'");
good("Ad Code Updated!");
}else{
	$data6['adcode_usr'] =  get_magic_quotes_gpc()==1 ? stripslashes($_POST['codead']) :$_POST['codead'];
$db->query_update("users_usr", $data6, "id_usr = '$_SESSION[user]'");

good("Ad Code Updated!");
	
				}}
		}
	}
	
	
function changepassword(){
		

		if($_POST['changepassword']){
			global $db;
dbconnect();
		$check = check_empty($_POST['password'] , "Enter your new password", $check);	
			if($check != "1"){
		$data6['password_usr'] = $_POST['password'];
	$db->query_update("users_usr", $data6, "id_usr = '$_SESSION[user]'");
good("Password Updated!");
			}}
	
	}



				function logout(){
				
session_destroy();

//  good($text);
redirect("index.php");
 
	
				}
			
			
function membermail(){
	

	if($_POST['submit']){
		global $db;
dbconnect();
$sql = "SELECT * FROM users_usr ";
$rows = $db->query($sql);



while ($record = $db->fetch_array($rows)) {
	$email = $record['email_usr'];
$name = $record['username_usr'];
	

		$to = $record['email_usr'];
		$sub = $_POST['subject'];
		$mes = $_POST['message'];
		
$subject = "$sub";
$message = "$mes";
$headers = 'From: stream4u.eu@gmail.com' . "\r\n" .
    'Reply-To: stream4u.eu@gmail.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

		$text = "email sent to $to";
		good($text);

	
		}
}
	}
	
function createchannel(){
	
	

	if($_POST['createchannel']){	global $db;
dbconnect();
	$check = check_empty($_POST['streamname'] , "Enter channel name", $check);	
	
	
	
	if($_POST['streamname']){
		$streamm = $db->escape($_POST[streamname]);	
$sql2 = "select u_stm from stream_stm where u_stm = '$streamm'";
$row = $db->query_first($sql2);

if (preg_match("@[^a-zA-Z0-9 ]@", $_POST['streamname'])) {
 error("Channel name should only be numbers and alphabets");
		 $check ="1";

} 


if ($row['u_stm']){
		 error("This channel is already present in our database.");
		 $check ="1";
		}







	}
if($check != "1"){
	
		$data6['u_stm'] = str_replace(" ", "",$_POST['streamname']);
			$data6['website_stm'] = NULL;
			$data6['uid_stm'] = $_SESSION['user'];
			$data6['p_stm'] = uniqid();
			$data6['date_stm'] = "NOW()";
			$data6['is_active_stm'] = 3;
			$data6['viewers'] = 0;
	$ids = $db->query_insert("stream_stm", $data6);
redirect("?p=viewchannel&id=$ids");
	
	}
	}
	}			
			
			
			
	function getchanneldetails($id){
		global $db;
dbconnect();

$sql = "SELECT * FROM stream_stm where id_stm = $id";
return $record = $db->query_first($sql); 

		
		}	
		
	
			
		// function listchannels(){
	// global $db;
// dbconnect();
// $sql = "select * from stream_stm as s left join users_usr as u on uid_stm = id_usr where uid_stm = $_SESSION[user]";
// $rows = $db->query($sql);
// while ($record= $db->fetch_array($rows)) {
// echo "<div class='left_channel'><a href='?p=viewchannel&id=$record[id_stm]'>$record[u_stm]</a></div><div class='right_channel'><a href='?p=dashboard&id=$record[id_stm]&s=del'> Delete</a></div>";
	// }

	// }
			
			
				
			function listwebsites(){
	global $db;
dbconnect();

	$sql = "select * from websitelist_wl where user_wl = $_SESSION[user]";
$rows = $db->query($sql);
while ($record= $db->fetch_array($rows)) {
	  echo "<div class='left_channel'>$record[website_wl]</div><div class='right_channel'><a href='?p=website&id=$record[id_wl]&s=del'> Delete</a></div>";
	}

	}
			
					function deletewebsite($id){
global $db;
dbconnect();
if(!$_POST){
$sql = "DELETE FROM websitelist_wl WHERE (id_wl = $id) and (user_wl = $_SESSION[user])";
$db->query($sql);

good("Deleted Successfully");
}

}	

			function deletestream($id){
global $db;
dbconnect();


$sql = "DELETE FROM stream_stm WHERE (id_stm = $id) and (uid_stm = $_SESSION[user])";
$db->query($sql);


}	

function getads(){
global $db;
dbconnect();

$sql = "SELECT adcode_usr FROM users_usr where id_usr = $_SESSION[user]";
return $record = $db->query_first($sql); 
}	
	
function getadsadmin($type){
global $db;
dbconnect();


$sql = "SELECT ad_ads FROM admin_ads where type_ads = '$type'";
return $record = $db->query_first($sql); 

}			
			
			

			
	

?>